﻿/*************************************************************************************
 *
 * 文 件 名:   CommonUtil
 * 描    述:   一些静态方法，函数
 * 
 * 版    本：  V1.0
 * 创 建 者：  YSW 
 * 创建时间：  2021-09-23 10:04:28
 * ======================================
 * 历史更新记录
 * 版本：V          修改时间：         修改人：
 * 修改内容：
 * ======================================
*************************************************************************************/

using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace GXRTBTC.PublicClassLibrary.Util
{
    /// <summary>
    /// 通用方法、函数
    /// 常量
    /// </summary>
    public class CommonUtil
    {
        /// <summary>
        /// 公共日志，请在Worker构造函数里面赋值
        /// </summary>
        public static ILogger mLogger { get; set; }
        /// <summary>
        /// 运行状态
        /// 0：正式环境
        /// 1：测试环境
        /// </summary>
        public static int runStatus { get; set; } = 0;
        /// <summary>
        /// 鉴权url
        /// </summary>

        public static string AddressIDS4 { get; set; } = "";

        /// <summary>
        /// 鉴权系统的角色
        /// </summary>
        public static Dictionary<string, string> mRoles { get; set; } = new Dictionary<string, string>()
        {
            {"10001", "书记"},
            {"10002", "副书记"},
            {"10003", "主任"},
            {"10004", "副主任"},
            {"10005", "总工程师"},
            {"10006", "纪委书记"},
            {"10007", "台长"},
            {"10008", "副台长"},
            {"10009", "专职监察干部"},
            {"10010", "总支部书记"},
            {"10011", "站长"},
            {"10012", "副站长"},
            {"10013", "支部书记"},
            {"10014", "值班组长"},
            {"10015", "值班人员"},
            {"10016", "技术专员"},
            {"10017", "维护部主任"},
            {"10018", "维护部副主任"},
            {"10019", "superadmin"}
        };

        /// <summary>
        /// 获取分中心领导
        /// 其实是分中心及以上的全部返回
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetSubLeader()
        {
            return mRoles.Where(d => int.Parse(d.Key) <= 10013).ToDictionary(d => d.Key, d => d.Value);
        }

        /// <summary>
        /// 获取维护部及以下联系人
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetDeptOper()
        {
            return mRoles.Where(d => int.Parse(d.Key) > 10013 && int.Parse(d.Key) != 10019).ToDictionary(d => d.Key, d => d.Value);
        }

        /// <summary>
        /// 基本告警字典
        /// 需要返回前台的数据
        /// </summary>
        public static Dictionary<string, string> mAlarmInfo { get; set; } = new Dictionary<string, string>()
        {
            {"alarm_key", ""},          //告警ID
            {"parentAlarm_key", ""},    //告警父级ID
            {"alarmCode", ""},          //告警唯一key
            {"alarm_name", ""},         //告警名称
            {"alarm_title", ""},        //告警标题
            {"alarm_title_id", ""},     //告警标题ID alarm_title_table表alarm_title_id
            {"alarm_desc", ""},         //告警值描述
            {"alarm_level", ""},        //告警级别
            {"real_level", ""},         //告警级别 当前实际
            {"alarm_value", ""},        //告警值 即该告警字段当前的值
            {"get_time", ""},           //告警时间

            {"isEnd", false.ToString()},    //告警是否结束
            {"isInTime", true.ToString()},  //是否在时间表内（生成告警时是否在时间表内）
            {"alarm", true.ToString()},     //需要推送到前端（实时是否在时间表内）
            {"clear", false.ToString()},    //是否告警清除
            {"isUpdate", false.ToString()}, //是否需要更新
            {"end_time", ""},               //告警结束时间

            {"send_status", "未派单"},     //派单状态
            {"dept_id", ""},            //维护部编号
            {"dept_name", ""},          //维护部名称

            {"alarm_id", ""},           //alarm_basic_info表id，即告警编号
            {"alarm_type", ""},         //告警类型
            {"station_id", ""},         //台站编号
            {"station_name", ""},       //台站名称
            {"subcenter_id", ""},       //分中心ID
            {"subcenter_name", ""},     //分中心名称
            {"station_level", ""},      //台站级别
            {"position_id", ""},        //工位号
            {"rposition_id", ""},        //关联工位号
            {"frequency", ""},          //频率
            {"sub_system_id", ""},      //子系统编号
            {"sub_system", ""},         //子系统名称
            {"device_type_id", ""},     //设备ID
            {"device_type_name", ""},   //设备名称
            {"program_name", ""},       //节目名称
            {"program_summary", ""},    //节目类型
            {"program_level", ""},      //节目级别
            {"device_type", ""},        //设备编码
            {"rdevice_type", ""},        //关联设备编码
            {"cover_mode", ""},         //覆盖方式

            {"tip_level", ""},          //告警提示音级别，1、2、3提示，其它不提示
            {"tip_level_max", ""},      //告警提示音级别，tip_level实时最大提示级别
            {"device_sn", ""},          //设备序列号

            {"loopAlarm", ""},          //固定月末（循环）停播标识
            {"onceStationAlarm", ""},   //动态（参数录入）停播标识，用于台站因各种原因随时可能产生的停播
            {"onceFreqAlarm", ""},      //动态（参数录入）停播标识，用于频率因各种原因随时可能产生的停播

            {"schemaName", ""},         //采集表名称
            {"parno", ""},              //告警字段采集表匹配名称
            {"parno_alias", ""},        //告警字段采集表匹配名称

            {"alarm_locate", ""},       //网元名称 即告警明细，让客户端值班人员能快速定位到设备、节目的备注信息
            {"alarm_hash", ""}          //告警哈希值，用于返回告警给前端时判断该条告警前后是否有变化
        };

        /// <summary>
        /// mAlarmInfo的简化版，用于返回前端时节约网络资源
        /// 一对一对应，value是简写
        /// 不需要返回的屏蔽掉
        /// </summary>
        public static Dictionary<string, string> mSimpleAlarmInfo { get; set; } = new Dictionary<string, string>()
        {
            {"alarm_key", "key"},           //告警ID
            {"parentAlarm_key", "p_k"},     //告警父级ID
            {"alarmCode", "al_c"},          //告警唯一key
            //{"alarm_name", "al_n"},       //告警名称
            {"alarm_title", "al_t"},        //告警标题
            {"alarm_title_id", "al_ti"},    //告警标题ID alarm_title_table表alarm_title_id
            //{"alarm_desc", "al_d"},       //告警值描述
            {"alarm_level", "al_l"},        //告警级别
            {"real_level", "r_al"},         //告警级别 当前实际
            //{"alarm_value", "al_v"},      //告警值 即该告警字段当前的值
            {"get_time", "gt"},             //告警时间

            {"isEnd", "end"},    //告警是否结束
            {"isInTime", "it"},  //是否在时间表内
            {"clear", "cl"},    //是否告警清除
            {"isUpdate", "upd"},     //是否在时间表内
            {"end_time", "et"},           //告警结束时间

            {"send_status", "ss"},       //派单状态
            {"dept_id", "dp_i"},            //维护部编号
            {"dept_name", "dp_n"},          //维护部名称

            {"alarm_id", "al_i"},           //alarm_basic_info表id，即告警编号
            {"alarm_type", "al_tp"},         //告警类型
            {"station_id", "sta_i"},         //台站编号
            {"station_name", "sta_n"},       //台站名称
            {"subcenter_id", "sub_i"},       //分中心ID
            {"station_level", "sta_l"},      //台站级别
            {"position_id", "pos_i"},        //工位号
            {"frequency", "freq"},          //频率
            {"sub_system_id", "sub_si"},      //子系统编号
            {"sub_system", "sub_s"},         //子系统名称
            {"device_type_id", "dev_ti"},     //设备ID
            {"device_type_name", "dev_tn"},   //设备名称
            {"program_name", "pg_n"},       //节目名称
            //{"program_summary", "pg_s"},    //节目类型
            {"program_level", "pg_l"},      //节目级别
            {"device_type", "dev_t"},         //设备编码
            {"cover_mode", "cov_m"},         //覆盖方式

            //{"schemaName", "sn"},         //采集表名称
            //{"parno", "pno"},         //告警字段采集表匹配名称
            //{"parno_alias", "pno_a"},        //告警字段采集表匹配名称

            {"alarm_locate", "al_lc"}         //网元名称 即告警明细，让客户端值班人员能快速定位到设备、节目的备注信息
            //{"alarm_hash", "ha"}             //告警哈希值，用于返回告警给前端时判断该条告警前后是否有变化
        };

        /// <summary>
        /// 告警历史表字段
        /// </summary>
        public static Dictionary<string, string> mAlarmHisCols { get; set; } = new Dictionary<string, string>()
        {
            {"ts","TIMESTAMP"},
            {"alarm_key","BINARY(32)"},
            {"isInTime","BOOL"},
            {"alarm","BOOL"},
            {"clear","BOOL"},
            {"get_time","BINARY(50)"},
            {"upd_time","BINARY(50)"},
            {"parentAlarm_key","BINARY(32)"},
            {"alarm_code","NCHAR(100)"},
            {"alarm_name","NCHAR(200)"},
            {"alarm_title","NCHAR(200)"},
            {"alarm_desc","NCHAR(255)"},
            {"alarm_level","INT"},
            {"real_level","INT"},
            {"alarm_value","BINARY(20)"},
            {"alarm_id","INT"},
            {"alarm_type","INT"},
            {"station_id","BINARY(15)"},
            {"station_name","NCHAR(100)"},
            {"subcenter_id","BINARY(15)"},
            {"dept_id","BINARY(15)"},
            {"station_level","INT"},
            {"position_id","BINARY(20)"},
            {"frequency","BINARY(20)"},
            {"sub_system_id","INT"},
            {"device_type_id","INT"},
            {"program_name","NCHAR(100)"},
            {"program_summary","NCHAR(100)"},
            {"program_level","INT"},
            {"device_type","BINARY(8)"},
            {"alarm_duration","BINARY(32)"},
            {"schemaname","BINARY(100)"},
            {"parno","BINARY(50)"},
            {"parno_alias","BINARY(50)"},
            {"alarm_locate","NCHAR(200)"}
        };

        /// <summary>
        /// 告警历史表字段
        /// 达梦
        /// </summary>
        public static Dictionary<string, string> mAlarmHisColsDM { get; set; } = new Dictionary<string, string>()
        {
            {"ts","timestamp default now()"},
            {"alarm_key","VARCHAR(32)"},
            {"isInTime","bit"},
            {"alarm","bit"},
            {"clear","bit"},
            {"get_time","VARCHAR(50)"},
            {"upd_time","VARCHAR(50)"},
            {"parentAlarm_key","VARCHAR(32)"},
            {"alarm_code","VARCHAR(100)"},
            {"alarm_name","VARCHAR(200)"},
            {"alarm_title","VARCHAR(200)"},
            {"alarm_desc","VARCHAR(255)"},
            {"alarm_level","INT"},
            {"real_level","INT"},
            {"alarm_value","VARCHAR(20)"},
            {"alarm_id","INT"},
            {"alarm_type","INT"},
            {"station_id","VARCHAR(15)"},
            {"station_name","VARCHAR(100)"},
            {"subcenter_id","VARCHAR(15)"},
            {"dept_id","VARCHAR(15)"},
            {"station_level","INT"},
            {"position_id","VARCHAR(20)"},
            {"frequency","VARCHAR(20)"},
            {"sub_system_id","INT"},
            {"device_type_id","INT"},
            {"program_name","VARCHAR(100)"},
            {"program_summary","VARCHAR(100)"},
            {"program_level","INT"},
            {"device_type","VARCHAR(8)"},
            {"alarm_duration","VARCHAR(32)"},
            {"schemaname","VARCHAR(100)"},
            {"parno","VARCHAR(50)"},
            {"parno_alias","VARCHAR(50)"},
            {"alarm_locate","VARCHAR(200)"}
        };

        /// <summary>
        /// 打印日志
        /// </summary>
        /// <param name="msg"></param>
        public static void LogInformation(string msg)
        {
            if (mLogger == null)
            {
                Console.WriteLine(msg);
            }
            else
            {
                mLogger.LogInformation(msg);
            }
        }

        /// <summary>
        /// 打印错误日志
        /// </summary>
        /// <param name="msg"></param>
        public static void LogError(string msg)
        {
            if (mLogger == null)
            {
                Console.WriteLine(msg);
            }
            else
            {
                mLogger.LogError(msg);
            }
        }
    }
}
